package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public R deleteById(Long id) {
        //1.判断该分类是否有菜品
//        Dish dish = dishMapper.selectById(id);
//        if (dish != null) {
//            return R.error("菜品分类不可以删除！");
//        }
//        //2.判断分类下是否有套餐
//        Setmeal setmeal = setmealMapper.selectById(id);
//        if (setmeal != null) {
//            return R.error("套餐分类不可以删除！");
//        }
//        //都不是，进行删除
//        int deleteById = categoryMapper.deleteById(id);
//        return deleteById>0?R.success("删除成功！"):R.error("删除失败！");

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId, id);
        List<Dish> dishes = dishMapper.selectList(qw);
        if (dishes != null) {
            return R.error("菜品分类不可以删除！");
        }
        LambdaQueryWrapper<Setmeal> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(Setmeal::getCategoryId, id);
      //  Setmeal setmeal = setmealMapper.selectOne(qw2);
        List<Setmeal> setmeals = setmealMapper.selectList(qw2);
        if (setmeals != null) {
            return R.error("套餐分类不可以删除！");
        }
        int deleteById = categoryMapper.deleteById(id);
        return deleteById > 0 ? R.success("删除成功！") : R.error("删除失败！");

    }
}
